<?php

declare(strict_types=1);

namespace App\Controller\Topic;


use App\Controller\BaseController;
use App\Request\Topic\TopicRequest;
use App\Services\Topic\TopicService;
use App\Utils\LoggerUtil;
use Hyperf\Context\Context;
use Hyperf\Di\Annotation\Inject;
use Psr\Http\Message\ResponseInterface;

class TopicController extends BaseController
{
    #[Inject]
    public TopicService $topicService;

    /**
     * Note - 推荐、或者关注用户的帖子列表
     * DateTime 2024/7/22 11:39
     * User Admin
     * @param TopicRequest $request
     * @return ResponseInterface
     */
    public function list(TopicRequest $request)
    {
        $params = $this->request->post();
        $request->execute($params, 'list');
        $user = Context::get('user');
        $topicList = $this->topicService->getTopicList($user, $params);
        return $this->return($topicList);
    }

    /**
     * Note - 创建帖子
     * DateTime 2024/7/22 11:48
     * User Admin
     * @return ResponseInterface
     */
    public function create(TopicRequest $request)
    {
        // $request = $this->container->get(TopicRequest::class);
        $user = Context::get('user');
        $params = $this->request->post();
        $request_type = $params['request_type'];
        if ($request_type == 2) {
            // $request->scene('check')->validateResolved();
            $request->execute($params, 'check');
            $res = $this->topicService->checkCreateStatus($user, $params);
        } else {
            // $request->scene('create')->validateResolved();
            $request->execute($params, 'create');
            $res = $this->topicService->createTopic($user, $params);
        }
        return $this->return($res);


    }

    /**
     * Note - 帖子详情
     * DateTime 2024/7/22 11:48
     * User Admin
     * @param TopicRequest $request
     * @return ResponseInterface
     */
    public function detail(TopicRequest $request)
    {
        // $request = $this->container->get(TopicRequest::class);
        // $request->scene('detail')->validateResolved();
        $params = $this->request->post();
        $request->execute($params, 'detail');
        $user = Context::get('user');
        $data = $this->topicService->getTopicDetail($user, $params);
        return $this->return($data);
    }

    /**
     * Note - 删除帖子
     * DateTime 2024/7/22 17:28
     * User Admin
     * @return ResponseInterface
     */
    public function delete(TopicRequest $request)
    {
        // $request = $this->container->get(TopicRequest::class);
        // $request->scene('delete')->validateResolved();
        $params = $this->request->post();
        $request->execute($params, 'delete');
        $user = Context::get('user');
        $topic_ids = $params['topic_id'];
        LoggerUtil::write('deleteTopic', $topic_ids);
        return $this->return($this->topicService->delete($user['id'], $topic_ids));
    }


}
